/**
 * FileName: SqlDb.cpp
 * Author:   kqbi
 * Date:     2019.7.12
 */
#include "SqlDb.h"
#include "Utils/Logger.h"

//## class SqlDb
SqlDb::SqlDb() : _connected(false) {
}

SqlDb::~SqlDb() {
}

bool SqlDb::isConnected() const {
    return _connected;
}

bool SqlDb::isSane() {
    return _connected;
}

void SqlDb::setConnected(bool connected) const {
    _connected = connected;
}

const char* SqlDb::tableName(const AbstractDb::Table& table) const {
    if (table > MaxTable)
        resip_assert(0);
    return tableNames[table];
}

bool SqlDb::dbEraseRecord(const Table table, const resip::Data& key, bool isSecondaryKey) {
    //#[ operation dbEraseRecord(const Table,const resip::Data&,bool)
    resip::Data command;
    {
        resip::DataStream ds(command);
        resip::Data escapedKey;
        ds << "DELETE FROM " << tableName(table);
        switch (table) {
        case PlateInfoTable:
            ds << " WHERE ID ='" << key << "'";
            break;
        default:
            break;
        }
    }
    if (query(command)) {
        S_LOG_ERROR("dbEraseRecord failed!");
        return false;
    }
    return true;
    //#]
}

